package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDashboardService;

/**
 * 仪表盘数据服务实现类
 * 
 * @author ruoyi
 */
@Service
public class SysDashboardServiceImpl implements ISysDashboardService
{
    @Autowired
    private SysUserMapper userMapper;
    
    private Random random = new Random();

    /**
     * 获取数据大屏统计数据
     * 
     * @return 统计数据
     */
    @Override
    public Map<String, Object> getDataScreenStats()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 用户总数
        Map<String, Object> countParams = new HashMap<>();
        int userCount = userMapper.selectUserCount(countParams);
        data.put("userCount", userCount);
        
        // 今日新增用户
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", DateUtils.getDate() + " 00:00:00");
        params.put("endTime", DateUtils.getDate() + " 23:59:59");
        int todayNewUsers = userMapper.selectUserCount(params);
        data.put("todayNewUsers", todayNewUsers);
        
        // 用户趋势数据（最近6个月）
        Map<String, Object> userTrend = generateUserTrendData();
        data.put("userTrend", userTrend);
        
        // 地区分布数据
        List<Map<String, Object>> regionData = generateRegionData();
        data.put("regionData", regionData);
        
        // 访问渠道数据
        List<Map<String, Object>> channelData = generateChannelData();
        data.put("channelData", channelData);
        
        // 排行榜数据
        List<Map<String, Object>> rankingList = generateRankingList();
        data.put("rankingList", rankingList);
        
        return data;
    }

    /**
     * 获取数据大屏实时数据
     * 
     * @return 实时数据
     */
    @Override
    public Map<String, Object> getDataScreenRealtime()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 在线用户数（示例数据）
        data.put("onlineUsers", 100 + random.nextInt(50));
        
        // 系统负载（示例数据）
        data.put("systemLoad", 30 + random.nextDouble() * 40);
        
        // 内存使用率（示例数据）
        data.put("memoryUsage", 50 + random.nextDouble() * 30);
        
        return data;
    }
    
    /**
     * 生成用户趋势数据（最近6个月）
     */
    private Map<String, Object> generateUserTrendData() {
        Map<String, Object> result = new HashMap<>();
        List<String> dates = new ArrayList<>();
        List<Integer> newUsers = new ArrayList<>();
        List<Integer> activeUsers = new ArrayList<>();
        
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -5); // 从6个月前开始
        
        for (int i = 0; i < 6; i++) {
            cal.set(Calendar.DAY_OF_MONTH, 1);
            String monthStr = (cal.get(Calendar.MONTH) + 1) + "月";
            dates.add(monthStr);
            
            // 模拟数据
            newUsers.add(100 + random.nextInt(200));
            activeUsers.add(200 + random.nextInt(200));
            
            cal.add(Calendar.MONTH, 1);
        }
        
        result.put("dates", dates);
        result.put("newUsers", newUsers);
        result.put("activeUsers", activeUsers);
        
        return result;
    }
    
    /**
     * 生成地区分布数据
     */
    private List<Map<String, Object>> generateRegionData() {
        List<Map<String, Object>> result = new ArrayList<>();
        
        String[] regions = {"华东", "华北", "华南", "西南", "西北"};
        int[] values = {1048, 735, 580, 484, 300};
        
        for (int i = 0; i < regions.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("name", regions[i]);
            item.put("value", values[i]);
            result.add(item);
        }
        
        return result;
    }
    
    /**
     * 生成访问渠道数据
     */
    private List<Map<String, Object>> generateChannelData() {
        List<Map<String, Object>> result = new ArrayList<>();
        
        String[] channels = {"微信公众号", "网站", "APP", "其他"};
        int[] values = {1548, 735, 580, 300};
        
        for (int i = 0; i < channels.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("name", channels[i]);
            item.put("value", values[i]);
            result.add(item);
        }
        
        return result;
    }
    
    /**
     * 生成排行榜数据
     */
    private List<Map<String, Object>> generateRankingList() {
        List<Map<String, Object>> result = new ArrayList<>();
        
        String[] functions = {"用户管理", "系统监控", "数据分析", "文件管理", "权限管理", "字典管理", "日志查询", "参数设置"};
        int[] values = {12361, 10252, 9385, 8473, 7564, 6852, 5341, 4213};
        
        for (int i = 0; i < functions.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("name", functions[i]);
            item.put("value", values[i]);
            result.add(item);
        }
        
        return result;
    }
} 